
import { getOrderList } from '@src/api/catering'
import router from "@src/router";
Component({
  behaviors: [router.routerBehavior],
  data: {
    navList: [
      { title: '全部' },
      { title: '待付款' },
      { title: '待配送' },
      { title: '已完成' },
      // { title: '已退款' },
      { title: '取消/退款' },
    ],
    activeNav: 0,
    pageNum: 1,
    pageSize: 10,
    lock: false,
    baseUrl: '',
    orderList: [],
    listQuery: []
  },
  methods: {
    onLoad() {
      this.setData({baseUrl: getApp().globalData.extConfig.fileBaseUrl })
      this.getOrderList()
    },
    onReachBottom() {
      if(this.data.lock) return
      this.getOrderList(this.data.listQuery)
    }, 
    onPullDownRefresh(){
      wx.showNavigationBarLoading()
      const sourceMap = {
        0: [],
        1: [1,2],
        2: [10],
        3: [3],
        4: [4,5,9]
      }
      this.getOrderList(sourceMap[this.data.activeNav])
      setTimeout(() => {
        wx.hideNavigationBarLoading();//完成停止加载
        wx.stopPullDownRefresh(); //得到数据后停止下拉刷新
      }, 400)
      console.log('下拉刷新成功')
    },
    getOrderList(orderStatus) {
      if(this.data.lock) return
      getOrderList({
        pageNum: this.data.pageNum,
        pageSize: 10,
        orderStatus
      })
      .then(res => {
        const { row } = res.data
        console.log(row)
        row.length >= this.data.pageSize ? this.setData({pageNum: ++this.data.pageNum }) : this.setData({lock:  true})
        this.setData({orderList: [...this.data.orderList, ...row]})
      })
    },
    initBaseData() {
      this.setData({
        pageNum: 1,
        lock: false,
        orderList: []
      })
    },
    navChange({target}) {
      const sourceMap = {
        0: [],
        1: [1,2],
        2: [10],
        3: [3],
        4: [4,5,9]
      }
      const { index } = target.dataset
      const { activeNav } = this.data
      if(index === activeNav) return
      this.initBaseData()
      this.setData({activeNav: index, listQuery: sourceMap[index]})
      this.getOrderList(sourceMap[index])
    },
  }
})
